// 与浏览器相关的工具类
export function isWechart(): boolean {
  const ua = window.navigator.userAgent.toLowerCase();
  if(ua.match(/MicroMessenger/i) + '' == 'micromessenger'){
    return true;
  }else{
    return false;
  }
}

export function getQueryString(name: string): string {
  if (name === undefined || name === null) {
    return null;
  }
  let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  let r = window.location.search.substr(1).match(reg);
  if (r === undefined || r === null) {
    r = window.location.hash.substr(1).match(reg);
  }
  if (r !== null) {
    return decodeURI(r[2]);
  }
  return null;
}

export function getCookie(name: string): string {
  const reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
  const arr = document.cookie.match(reg);
  if (arr) {
    return decodeURI(arr[2]);
  }
  return null;
}

export function setCookie(name: string, value: string, domain: string): void {
  var Days = 30;
  let exp: Date = new Date();
  exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  document.cookie = [
    `${name}=${encodeURI(value)}`,
    `;expires=${exp.toUTCString()}`,
    domain ? `;domain=${domain}` : ''
  ].join('');
}

export function delCookie(name: string): void {
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  var cval = getCookie(name);
  if (cval !== null) {
    document.cookie = name + '=' + cval + ';expires=' + exp.toUTCString();
  }
}

export function existSessionStorage() {
  try {
    return ('sessionStorage' in window) && window.sessionStorage &&
      window.sessionStorage !== null;
  } catch (e) {
    return false;
  }
}

export function setValueInSession(key: string, value: string): void {
  sessionStorage.setItem(key, value);
}

export function getValueInSession(key: string): string {
  return sessionStorage.getItem(key);
}

export function removeValueInSession(key: string): void {
  sessionStorage.removeItem(key);
}
